package de.tabuma.projectview.ribbon;

import java.util.*;
import de.tabuma.projectview.constants;
import de.tabuma.pm.externalcontent.logik.*;
import de.tabuma.pm.externalcontent.entity.*;
import de.tabuma.pm.htmlcontent.logik.*;
import de.tabuma.pm.htmlcontent.entity.*;
import de.tabuma.pm.projectnavigation.entity.*;
import de.tabuma.pm.projectnavigation.logik.*;
import javax.servlet.http.*;

/**
 *
 * @author alefen
 */
public class ContentFacade {
    public static String getContent(HttpServletRequest request, int projectId) {
        String str = "";
        str += printIframe();
        List<ProjectNavigation> list = ProjectNavigationFacade.getList(projectId);
        for(int i=0;i<list.size();i++) {
            ProjectNavigation pn = list.get(i);
            str += getFunction(request, pn);
        }
        return str;
    }

    public static String getFunction(HttpServletRequest request, ProjectNavigation pn) {
        String str = "\t\tfunction "+constants.functionName+pn.getId()+"() {\n";

        switch(pn.getNavMode()) {
            case de.tabuma.pm.constants.ProjectNavigationExternalContent:
                ExternalContent ec = ExternalContentFacade.getWithNavigationId(pn.getId().intValue());
                str += "\t\t\t"+constants.printLoginResponseFunction+ec.getId()+"();\n";
                break;

            case de.tabuma.pm.constants.ProjectNavigationHtmlContent:
                HtmlContent hc = HtmlContentFacade.getFromProjectnavigationId(pn.getId().intValue());
                if(hc!=null) {
                    str += "\t\t\tdocument.getElementById('"+constants.contentIframe+"').style.visibility='hidden';\n";
                    str += "\t\t\tdocument.getElementsByName('"+constants.contentIframe1+"')[0].style.visibility='hidden';\n";
                    String form = hc.getText().getValue().replaceAll("\\n","");
                    form = form.replaceAll("[\\t\\n\\x0B\\f\\r]","");
                    str += "\t\t\tcontent = '"+form+"';\n";
                    str += "\t\t\t"+constants.printIFrame+"(content);\n";
                }
                break;
        }
        str += "\t\t\tdocument.getElementById(\""+constants.breadcrumpsElement+"\").innerHTML = '"+getBreadcrump(pn)+"';\n";
        str+= "\t\t}\n";
        return str;
    }

    public static String getBreadcrump(ProjectNavigation pn) {
        String breadcrump = pn.getName();
        if(pn.getNavigationId()!=0) {
            ProjectNavigation pn1 = ProjectNavigationFacade.get(pn.getNavigationId());
            breadcrump = getBreadcrump(pn1) + " > " + pn.getName();
        }
        return breadcrump;
    }

    public static String printIframe() {
        String str = "";
        str += "\t\tfunction " + constants.printIFrame + "(string) {\n";
        str += "\t\t\tstr = '';\n";
        str += "\t\t\tstr += '<html>\\n<head>\\n<title>dynamic content</title>\\n</head>\\n<body>' + string + '</body></html>';\n";
        str += "\t\t\tdocument.getElementById('"+constants.contentIframe+"').innerHTML=string;\n";
        str += "\t\t\tdocument.getElementById('"+constants.contentIframe+"').style.visibility='visible';\n";
        str += "\t\t\tdocument.getElementsByName('"+constants.contentIframe1+"')[0].style.visibility='hidden';\n";
        str += "\t\t}\n";
        return str;
    }
}